package cn.com.controller.manageController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Embryo;
import cn.com.db.service.EmbryoService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Auth(UserAuth.ADMIN)
@RequestMapping("/back/embryo")
@RestController
public class EmbryoManageController {

    @Autowired
    EmbryoService embryoService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String createEmbryo(Embryo embryo){
        DataResult dataResult=new DataResult();
        embryo.setId(UidUtil.createUUID());
        embryo.setCreated_at(TimeUtil.getNowTimestamp());
        embryo.setUpdated_at(TimeUtil.getNowTimestamp());
        embryoService.createEmbryo(embryo);
        dataResult.setData(embryo);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("添加成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getEmbryo(){
        DataResult dataResult=new DataResult();
        dataResult.setTotal(embryoService.countEmbryo());
        dataResult.setData(embryoService.getEmbryo());
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public String viewEmbryo(@PathVariable("id")String id){
        DataResult dataResult=new DataResult();
        if (!embryoService.isExist(id)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("蛋糕胚不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setData(embryoService.getEmbryoById(id));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("获取成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Embryo embryo){
        DataResult dataResult=new DataResult();
        if (!embryoService.isExist(embryo.getId())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("蛋糕胚不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        embryoService.updateEmbryo(embryo);
        dataResult.setData(embryoService.getEmbryoById(embryo.getId()));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("编辑成功");
        return JsonUtil.toJsonString(dataResult);
    }



}
